package com.yinhai.uid.utils;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author taojie
 * @Date: 2018/12/30 12:08
 * @Description:
 */
@Component
public class ServiceInfoUtil implements ApplicationListener<WebServerInitializedEvent> {
	private static WebServerInitializedEvent event;

	@Override
	public void onApplicationEvent(WebServerInitializedEvent event) {
		ServiceInfoUtil.event = event;
	}

	public static int getPort() {
		Assert.notNull(event,"服务器初始化事件获取失败");
		int port = event.getWebServer().getPort();
		Assert.state(port != -1, "端口号获取失败");
		return port;
	}
}
